package com.example.swd1.gudong.main.user.mymedal;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.swd1.gudong.R;

public class UserMedalActivity extends Activity implements View.OnClickListener {
    private ImageView iv_user_medal_back;
    private RelativeLayout ll_user_medal_run;
    private TextView tv_user_medal_run_text;
    private TextView tv_user_medal_run_line;
    private RelativeLayout ll_user_medal_happy;
    private TextView tv_user_medal_happy_text;
    private TextView tv_user_medal_happy_line;
    private FrameLayout fl_medal;
    private boolean runTabChoosed;
    private boolean happyTabChoosed;
    private FragmentManager fm;
    private RunningMedalFragment runningMedalFragment;
    private HappyMedalFragment happyMedalFragment;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_medal);

        loadUIComponent();

    }

    private void loadUIComponent() {
        iv_user_medal_back = (ImageView) findViewById(R.id.iv_user_medal_back);
        ll_user_medal_run = (RelativeLayout) findViewById(R.id.ll_user_medal_run);
        tv_user_medal_run_text = (TextView) findViewById(R.id.tv_user_medal_run_text);
        tv_user_medal_run_line = (TextView) findViewById(R.id.tv_user_medal_run_line);
        ll_user_medal_happy = (RelativeLayout) findViewById(R.id.ll_user_medal_happy);
        tv_user_medal_happy_text = (TextView) findViewById(R.id.tv_user_medal_happy_text);
        tv_user_medal_happy_line = (TextView) findViewById(R.id.tv_user_medal_happy_line);
        fl_medal = (FrameLayout) findViewById(R.id.fl_medal);

        iv_user_medal_back.setOnClickListener(this);
        ll_user_medal_run.setOnClickListener(this);
        ll_user_medal_happy.setOnClickListener(this);

        fm = getFragmentManager();
        runningMedalFragment = new RunningMedalFragment();
        happyMedalFragment = new HappyMedalFragment();
        onClick(ll_user_medal_run);
    }

    @Override
    public void onClick(View v) {
        if (v == iv_user_medal_back) {
            finish();
            return;
        }
        if (v == ll_user_medal_run) {
            if (runTabChoosed == false) {
                if (happyTabChoosed) {
                    changeChoosedTab(tv_user_medal_happy_text, tv_user_medal_happy_line, false);
                }
                changeChoosedTab(tv_user_medal_run_text, tv_user_medal_run_line, true);
                runTabChoosed = true;
                happyTabChoosed = false;

                FragmentTransaction ft = fm.beginTransaction();
                ft.replace(R.id.fl_medal, runningMedalFragment);
                ft.commit();
            }
            return;
        }
        if (v == ll_user_medal_happy) {
            if (happyTabChoosed == false) {
                if (runTabChoosed) {
                    changeChoosedTab(tv_user_medal_run_text, tv_user_medal_run_line, false);
                }
                changeChoosedTab(tv_user_medal_happy_text, tv_user_medal_happy_line, true);
                runTabChoosed = false;
                happyTabChoosed = true;

                FragmentTransaction ft = fm.beginTransaction();
                ft.replace(R.id.fl_medal, happyMedalFragment);
                ft.commit();
            }
            return;
        }
    }

    private void changeChoosedTab(TextView text, TextView line, boolean state) {
        if (state) {
            text.setTextColor(getResources().getColor(R.color.green_grid));
            line.setVisibility(View.VISIBLE);
            line.setBackgroundColor(getResources().getColor(R.color.green_grid));
        } else {
            text.setTextColor(getResources().getColor(R.color.button_material_dark));
            line.setVisibility(View.INVISIBLE);
        }
    }
}
